<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>新增展位模板</span>
        <el-button
          style="float: right; padding: 0;margin-left:14px"
          type="text"
          @click="goto('/jobfair/positionTpl/list')"
        >
          返回
        </el-button>
        <el-button
          style="float: right; padding: 0;"
          type="text"
          @click="onSubmit('form')"
          :disabled="issubmit"
        >
          保存
        </el-button>
      </div>
      <el-form
        ref="form"
        class="common-form"
        :model="form"
        label-width="120px"
        :rules="rules"
        :inline-message="true"
      >
        <el-form-item label="模板名称" prop="title">
          <el-input v-model="form.title" />
        </el-form-item>
        <div v-for="(item, index) in form.area" :key="index">
          <el-form-item label="展区编号"  required>
            <el-select v-model="form.area[index]" placeholder="选择模板">
              <el-option
                v-for="(v, k) in area"
                :key="k"
                :label="v"
                :value="v"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="展位号" prop="position" class="is-required">
            <el-input v-model="form.position_start[index]" class="position" />
            至
            <el-input v-model="form.position_end[index]" class="position" />
          </el-form-item>
          <div class="border" />
        </div>
        <el-form-item label="">
          <el-button type="primary" @click="onSubmit('form')" :disabled="issubmit">保存</el-button>
          <el-button type="primary" @click="onArea">继续添加</el-button>
          <el-button @click="goto('/jobfair/positionTpl/list')">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { positionTplAdd } from '@/api/jobfair'
export default {
  data() {
    return {
      area: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'Z'],
      form: {
        title: '',
        area: [],
        position_start: [],
        position_end: []
      },
      issubmit: false,
      rules: {
        title: [
          { required: true, message: '请填写模板名称', trigger: 'blur' },
          { max: 200, message: '长度在 1 到 30 个字符', trigger: 'blur' }
        ],
        position: [
          { type: 'number', message: '请正确填写展位号', trigger: 'blur' }
        ]
      }
    }
  },
  created(){
    this.onArea()
  },
  methods: {
    onArea: function(){
      this.form.area.push('A')
      this.form.position_start.push('')
      this.form.position_end.push('')
    },
    onSubmit(formName) {
      this.issubmit = true
      const that = this
      var insertData = Object.assign({}, this.form)
      this.$refs[formName].validate(valid => {
        if (valid) {
          positionTplAdd(insertData).then(response => {
            this.$message.success(response.message)
            setTimeout(function() {
              that.$router.push('/jobfair/positionTpl/list')
            }, 1500)
            return true
          }).catch(() => {
            that.issubmit = false
          })
        } else {
          that.issubmit = false
          return false
        }
      })
    },
    goto(target) {
      this.$router.push(target)
    }
  }
}
</script>
<style scoped>
.border{border-top:1px dashed #eee;padding:20px 0}
.position{width:100px}
</style>
